package com.googlecode.lohr.tools.interpreter;

import lohr.lang.dom.Expression;
import lohr.lang.dom.Literal;
import lohr.lang.dom.MethodInvocation;
import lohr.lang.dom.ReferenceExpression;
import lohr.lang.dom.SlotDefinition;

public abstract class ExpressionImpl extends ObjectImpl {
	
	private Expression _expression;
	
	public static final ExpressionImpl createExpressionImpl(LohrInterpreter interpreter, Expression expression) {
		if (expression instanceof Literal) {
			return LiteralImpl.createLiteral(interpreter, (Literal)expression);
		}
		if (expression instanceof MethodInvocation) {
			return new MethodInvocationImpl(interpreter, (MethodInvocation)expression);
		}
		if (expression instanceof ReferenceExpression) {
			return new ReferenceExpressionImpl(interpreter, (ReferenceExpression) expression);
		}
		if (expression instanceof SlotDefinition) {
			return new SlotDefinitionImpl(interpreter, (SlotDefinition)expression);
		}
		throw new RuntimeException("Unknown expression type"+expression);
	}

	protected ExpressionImpl(LohrInterpreter interpreter, Expression expression) {
		super(interpreter, interpreter.ClosureType);
		_expression= expression;
	}

	abstract public ObjectImpl invoke(ObjectImpl invocationContext, Locals arguments);

	public Expression getExpression() {
		return _expression;
	}
	
	@Override
	public String toString() {
		return _expression.toString();
	}
}
